var lOptions = {
	_pref: null,

  setConf: function(setting){

    if(typeof(setting) != "object") return;

    document.getElementById('txtService').value = setting.hypertopic.url;
		document.getElementById('txtUser').value = setting.hypertopic.username;
		document.getElementById('txtPass').value = setting.hypertopic.password;

		document.getElementById('txtDavServer').value = setting.webdav.url;
		document.getElementById('txtDavUser').value = setting.webdav.username;
		document.getElementById('txtDavPass').value = setting.webdav.password;
  },

  getConf: function(){
    var stringBundle = document.getElementById("sbLasuli");
    var setting = {
      "hypertopic" :
      {
        "url" : "",
        "username" : "",
        "password" : ""
      },

      "webdav" :
      {
        "url" : "",
        "username" : "",
        "password" : ""
      }
    };
    setting.hypertopic.url = document.getElementById('txtService').value;
    setting.hypertopic.username = document.getElementById('txtUser').value;
    setting.hypertopic.password = document.getElementById('txtPass').value;
    setting.webdav.url = document.getElementById('txtDavServer').value;
    setting.webdav.username = document.getElementById('txtDavUser').value;
    setting.webdav.password = document.getElementById('txtDavPass').value;

    if(typeof(setting.hypertopic.url) != "string" || setting.hypertopic.url.trim() == ""){
			alert(stringBundle.getString("lasuliOptions.error.serviceisnull"));
			return null;
		}

		if(!setting.hypertopic.url.isUrl()){
			alert(stringBundle.getString("lasuliOptions.error.serviceisnotanurl"));
			return null;
		}

    if(typeof(setting.webdav.url) != "string" || setting.webdav.url.trim() == ""){
			alert(stringBundle.getString("lasuliOptions.error.wdisnull"));
			return null;
		}

		if(!setting.webdav.url.isUrl()){
			alert(stringBundle.getString("lasuliOptions.error.wdisnotanurl"));
			return null;
		}

		if(setting.webdav.url.substr(-1,1) != "/")
			setting.webdav.url += "/";
    if(setting.hypertopic.url.substr(-1,1) != "/")
			setting.hypertopic.url += "/";

	  return setting;
  },

	init: function(){
		window.sizeToContent();
		this._pref = new Preferences(PREF_PREFIX);

		var strSetting = this._pref.getPref("setting", "");
    if(strSetting == "")
      this.setConf(DEFAULT_SETTING);
    else{
      var setting = strSetting.parseJSON();
      this.setConf(setting);
    }
	},

	destory: function(){

	},

	setOptions: function(){
    var setting = this.getConf();
    if(typeof(setting) != "object")
      return false;

		this._pref.setPref("setting", setting.toJSONString());
		this._pref.savePrefFile();
	},


	saveConf: function(){
	  var stringBundle = document.getElementById("sbLasuli");
	  var confName = document.getElementById('txtTemplateName').value;
	  if(typeof(confName) != "string" || confName.trim() == ""){
	    alert(stringBundle.getString("lasuliOptions.error.templateNameIsNull"));
			return false;
		}
    var setting = this.getConf();
    if(typeof(setting) != "object")
      return false;
    this.saveHistory(confName);
    confName = MD5(confName);
    var pref = new Preferences(PREF_TEMPLATE_PREFIX);
		pref.setPref(confName, setting.toJSONString());
		pref.savePrefFile();
    alert(stringBundle.getString("lasuliOptions.info.templateSaved"));
	},

	removeConf: function(){
	  var stringBundle = document.getElementById("sbLasuli");
	  var confName = document.getElementById('txtTemplateName').value;
	  var pref = new Preferences(PREF_TEMPLATE_PREFIX);

	  if(typeof(confName) != "string" || confName.trim() == ""){
	    alert(stringBundle.getString("lasuliOptions.error.templateNameIsNull"));
			return false;
		}
		this.removeHistory(confName);
    confName = MD5(confName);
    pref.clearPref(confName);
		pref.savePrefFile();
	},

	loadConf: function(){
	  var stringBundle = document.getElementById("sbLasuli");
	  var confName = document.getElementById('txtTemplateName').value;
	  var pref = new Preferences(PREF_TEMPLATE_PREFIX);

	  if(typeof(confName) != "string" || confName.trim() == ""){
	    alert(stringBundle.getString("lasuliOptions.error.templateNameIsNull"));
			return false;
		}
    confName = MD5(confName);
    var strSetting = pref.getPref(confName, "");
    if(strSetting == ""){
      alert(stringBundle.getString("lasuliOptions.error.templateNameNotFound"));
			return false;
    }
    else{
      var setting = strSetting.parseJSON();
      this.setConf(setting);
    }
	},
	saveHistory: function(strName){
    var gFormHistory = Components.classes["@mozilla.org/satchel/form-history;1"].getService(Components.interfaces.nsIFormHistory ?
                                                                                            Components.interfaces.nsIFormHistory :
                                                                                            Components.interfaces.nsIFormHistory2);
    gFormHistory.addEntry("lasuli-template-search-history", strName);
    //alert(gFormHistory.hasEntries);
  },
  removeHistory: function(strName){
    var gFormHistory = Components.classes["@mozilla.org/satchel/form-history;1"].getService(Components.interfaces.nsIFormHistory ?
                                                                                            Components.interfaces.nsIFormHistory :
                                                                                            Components.interfaces.nsIFormHistory2);
    gFormHistory.removeEntry("lasuli-template-search-history", strName);
    //alert(gFormHistory.hasEntries);
  }
};